package com.ttn.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ttn.business.service.GradeService;
import com.ttn.common.core.entity.PageQuery;
import com.ttn.common.core.entity.Result;
import com.ttn.common.core.entity.business.Grade;
import com.ttn.common.core.entity.business.bo.ModifyCourseUnitSortBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 等级(StudentRecord)表控制层
 *
 * @author makejava
 * @since 2023-01-30 15:57:40
 */
@RestController
@Slf4j
@RequestMapping("grade")
public class GradeController {
    /**
     * 服务对象
     */
    @Resource
    private GradeService gradeService;

    /**
     * 保存等级信息
     *
     * @param request
     * @param grade
     * @return
     */
    @PostMapping("/save/grade")
    public Result saveGrade(HttpServletRequest request, @RequestBody Grade grade) {
        log.info("GradeController saveGrade ,参数:{}", grade);
        LambdaQueryWrapper<Grade> gradeLambdaQueryWrapper = new LambdaQueryWrapper();
        gradeLambdaQueryWrapper.eq(Grade::getGradeName, grade.getGradeName());
        //判断等级名称是否重复。不重重则直接保存
        if (gradeService.count(gradeLambdaQueryWrapper) > 0) {
            return Result.fail("级别重复,请确认后重试");
        }
        gradeService.save(grade);
        return Result.success();
    }

    /**
     * 修改级别
     *
     * @param request
     * @param grade
     * @return
     */

    @PutMapping("/modify/grade")
    public Result modifyGrade(HttpServletRequest request, @RequestBody Grade grade) {
        log.info("GradeController modifyGrade ,参数:{}", grade);
        LambdaQueryWrapper<Grade> gradeLambdaQueryWrapper = new LambdaQueryWrapper();
        gradeLambdaQueryWrapper.eq(Grade::getGradeName, grade.getGradeName());
        gradeLambdaQueryWrapper.ne(Grade::getId, grade.getId());
        //判断等级名称是否重复。不重重则直接保存
        if (gradeService.count(gradeLambdaQueryWrapper) > 0) {
            return Result.fail("级别重复,请确认后重试");
        }
        gradeService.updateById(grade);
        return Result.success();
    }

    /**
     * 分页查询级别配置
     *
     * @param request
     * @param pageQuery
     * @return
     */
    @PostMapping("/query/grade-page")
    public Result queryGradePage(HttpServletRequest request, @RequestBody PageQuery pageQuery) {
        log.info("GradeController queryGradePage ,参数:{}", pageQuery);
        IPage<Grade> gradePage = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        LambdaQueryWrapper<Grade> gradeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        gradeLambdaQueryWrapper.orderByAsc(Grade::getSort);
        return Result.success(gradeService.page(gradePage, gradeLambdaQueryWrapper));
    }


    /**
     * 修改排序
     *
     * @param request
     * @param modifyCourseUnitSortBO
     * @return
     */
    @PutMapping("/modify/grade-sort")
    public Result modifyGradeSort(HttpServletRequest request, @RequestBody ModifyCourseUnitSortBO modifyCourseUnitSortBO) {
        log.info("GradeController modifyGradeSort ,参数:{}", modifyCourseUnitSortBO);
        Grade grade = gradeService.getById(modifyCourseUnitSortBO.getId());
        LambdaQueryWrapper<Grade> gradeLambdaQueryWrapper = new LambdaQueryWrapper();
        gradeLambdaQueryWrapper.ne(Grade::getId, modifyCourseUnitSortBO.getId());
        gradeLambdaQueryWrapper.orderByAsc(Grade::getSort);
        List<Grade> gradeList = gradeService.list(gradeLambdaQueryWrapper);
        if (gradeList.size() > 0) {
            int sortIndex = 0;
            if (modifyCourseUnitSortBO.getSortingBehavior().equals(-1)) {
                if (grade.getSort() - 2 > 0) {
                    sortIndex = grade.getSort() - 2;
                }
            } else if (modifyCourseUnitSortBO.getSortingBehavior().equals(1)) {
                sortIndex = grade.getSort();
            }
            gradeList.add(sortIndex, grade);
            AtomicInteger i = new AtomicInteger(1);
            gradeList.stream().forEach(grade1 -> {
                grade1.setSort(i.getAndIncrement());
            });
            gradeService.updateBatchById(gradeList);
        }
        return Result.success();
    }

    /**
     * 删除等级
     *
     * @param request
     * @param id
     * @return
     */

    @DeleteMapping("/delete/grade-id/{id}")
    public Result deleteGradeById(HttpServletRequest request, @PathVariable("id") Integer id) {
        log.info("GradeController deleteGradeById ,参数id:{}", id);
        Grade grade = gradeService.getById(id);
        gradeService.removeById(id);
        LambdaQueryWrapper<Grade> gradeLambdaQueryWrapper = new LambdaQueryWrapper();
        gradeLambdaQueryWrapper.ne(Grade::getId, id);
        gradeLambdaQueryWrapper.orderByAsc(Grade::getSort);
        List<Grade> gradeList = gradeService.list(gradeLambdaQueryWrapper);
        if (gradeList.size() > 0) {
            AtomicInteger i = new AtomicInteger(1);
            gradeList.stream().forEach(grade1 -> {
                grade1.setSort(i.getAndIncrement());
            });
            gradeService.updateBatchById(gradeList);
        }
        return Result.success();
    }

    @GetMapping("/service/query/grade-id/{id}")
    public Result serviceQueryGradeById(@PathVariable("id") Integer id) {
        log.info("GradeController serviceQueryGradeById ,参数id:{}", id);
        Grade grade = gradeService.getById(id);
        return Result.success(grade);
    }

}

